package com.binvshe.binvshe.mine.ticket;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.utils.FragmentUtils;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.frameworkbase.utils.DateTimeUtil;

import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/9/5
 */
public class TicketAdapter extends RecyclerViewBaseAdapter<TicketEntity> {

    private boolean mIsLoseTicket;

    private FragmentManager mFragmentManager;

    public TicketAdapter(Context con) {
        super(con);
    }

    public boolean getIsLoseTicket() {
        return mIsLoseTicket;
    }

    public void setIsLoseTicket(boolean IsLoseTicket) {
        this.mIsLoseTicket = IsLoseTicket;
    }

    public void setFragmentManager(FragmentManager IsLoseTicket) {
        this.mFragmentManager = IsLoseTicket;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        return new VH(setAdapterViewItem(parent, R.layout.item_ticket));
    }

    @Override
    public void onBindHolder(RecyclerView.ViewHolder holder, final int position) {
        final VH viewHolder = (VH) holder;
        final TicketEntity ticketEntity = getData().get(position);
        if (getIsLoseTicket()) {
            viewHolder.relytTicketContent.setBackgroundResource(R.mipmap.icon_ticket_go);
            viewHolder.tvTicketState.setText("立即使用");
            viewHolder.tvTicketState.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FragmentUtils.addFragment(mFragmentManager, R.id.mTicketContainer, ShowQrCodeFragment.newInstance(ticketEntity.getQrcode_id()));
                }
            });
        } else {
            viewHolder.relytTicketContent.setBackgroundResource(R.mipmap.icon_ticket_over);
            if (ticketEntity.isUsed()) {
                viewHolder.tvTicketState.setText("已使用");
            } else {
                viewHolder.tvTicketState.setText("已过期");
            }
        }

        if (!TextUtils.isEmpty(ticketEntity.getTicket().getName())){
            viewHolder.tvTicketTitle.setText(ticketEntity.getTicket().getName());
        }


        Date beginDate = new Date(ticketEntity.getTicket().getBegin_time());
        Date endDate = new Date(ticketEntity.getTicket().getEnd_time());
        viewHolder.tvTicketValue.setText("有效期 : "+DateTimeUtil.getDateTimeStr(beginDate) + " ~ " + DateTimeUtil.getDateTimeStr(endDate));


        String name = ticketEntity.getTicket().getActivity().getName();
        if (TextUtils.isEmpty(name)){
            viewHolder.tvTicketActivity.setText("已作废");
        }else{
            viewHolder.tvTicketActivity.setText(ticketEntity.getTicket().getActivity().getName());
        }

//
    }

    public class VH extends RecyclerView.ViewHolder {

        @Bind(R.id.tvTicketTitle)
        TextView tvTicketTitle;
        @Bind(R.id.tvTicketActivity)
        TextView tvTicketActivity;
        @Bind(R.id.tvTicketValue)
        TextView tvTicketValue;
        @Bind(R.id.tvTicketState)
        TextView tvTicketState;
        @Bind(R.id.relytTicketContent)
        RelativeLayout relytTicketContent;


        public VH(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
